package com.jcvision.mms.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jcvision.mms.dao.SysUserDao;
import com.jcvision.mms.model.SysUser;
import com.jcvision.mms.model.SysUserExample;

@Controller
@RequestMapping("/module/login")
public class LoginController extends BaseController {
	
	private static Logger logger = Logger.getLogger(LoginController.class);

	@SuppressWarnings("unchecked")
	@RequestMapping("/login.do")
	public String Login(SysUser sysUser, HttpSession session){
		if(sysUser != null && sysUser.getUsername() != null && sysUser.getPassword() != null){
			SysUserExample sysUserExample = new SysUserExample();
			sysUserExample.createCriteria().andUsernameEqualTo(sysUser.getUsername()).andPasswordEqualTo(sysUser.getPassword());
			SysUserDao sysUserDao = new SysUserDao();
			List<SysUser> users = (List<SysUser>) sysUserDao.select(sysUserExample);
			if(users != null && users.size() > 0){
				sysUser = users.get(0);
				session.setAttribute("sysUser", sysUser);
				logger.info("用户 " + sysUser.getUsername() + " 登录成功");
				return "redirect:/module/index/home.jsp";
			}
		}
		return "redirect:/module/login/login.jsp";
	}
	
}
